<?php

namespace app\admin\controller\content\files;

use app\admin\controller\Controller;
use app\admin\model\{UploadFile,UploadGroup as GroupModel};

/**
 * 文件分组
 * Class Group
 * @package app\store\controller\content
 */
class Group extends Controller
{

    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function initialize()
    {
        parent::initialize();
        // 控制器初始化
        $this->model = new GroupModel();
    }
    /**
     * 文件分组列表
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = $this->model->getList()->toArray();
        array_unshift($data,['group_id'=>0,'group_type'=>'image','group_name'=>'未分组','sort'=>'1','create_time'=>date('Y-m-d H:i:s'),'update_time'=>date('Y-m-d H:i:s'),'is_delete'=>0]);
        array_push($data,['group_id'=>-1,'group_type'=>'image','group_name'=>'回收站','sort'=>'1','create_time'=>date('Y-m-d H:i:s'),'update_time'=>date('Y-m-d H:i:s'),'is_delete'=>0]);
        return $this->renderSuccess($data);
    }

    /**
     * 添加文件分组
     * @return array|mixed
     */
    public function add()
    {
        // 新增记录
        if ($this->model->add($this->request->param())) {
            return $this->renderSuccess($this->model,'添加成功');
        }
        return $this->renderError($this->model->getError() ?? '添加失败');
    }

    /**
     * 编辑文件分组
     * @param $group_id
     * @return array|mixed
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        // 更新记录
        if ($this->model->edit($this->request->param())) {
            return $this->renderSuccess('更新成功');
        }
        return $this->renderError($this->model->getError() ?? '更新失败');
    }

    /**
     * 删除文件分组
     * @param $group_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete()
    {
        if (!$this->model->remove($this->request->param('group_id'))) {
            return $this->renderError($this->model->getError() ?? '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

    
    /**
     * 批量移动文件分组
     * @param $group_id
     * @param $fileIds
     * @return array
     */
    public function moveFiles()
    {
        $model = new UploadFile;
        if ($model->moveGroup($this->request->param('group_id'),$this->request->param('file_ids/a')) !== false) {
            return $this->renderSuccess([],'移动成功');
        }
        return $this->renderError($this->model->getError() ?: '移动失败');
    }

}
